import CalModule as CalModule
import pandas as pd
import pymssql
import matplotlib.pyplot as plt


if __name__ == "__main__":
    conn = pymssql.connect(server='127.0.0.1', database='SPDB')
    # df = pd.read_csv(r"D:\BaiduNetdiskDownload\20161003\20161003_lonlattr.csv")
    df = pd.read_csv(r"D:\BaiduNetdiskDownload\20190429\20190429_trajectory.txt")
    # with open(r"D:\BaiduNetdiskDownload\20161003\20161003_proj_dist.txt", "w") as f:
    with open(r"D:\BaiduNetdiskDownload\20190429\20190429_proj_dist.txt", "w") as f:
        f.write("trueroad,dist\n")
        for i in range(df.shape[0]):
            trueroad = df['trueroad'].iloc[i]
            lon = df['lon'].iloc[i]
            lat = df['lat'].iloc[i]
            wkt_df = pd.read_sql(f"select shape.STAsText() wkt from shroads3 where id = {trueroad}", conn)
            wkt = wkt_df['wkt'].iloc[0]
            pt_arr = CalModule.get_ptarray(wkt)
            x, y, _, min_dist_proj, _ = CalModule.projection_pl(lon, lat, pt_arr)
            plt.plot(pt_arr[:, 0], pt_arr[:, 1])
            plt.scatter(lon, lat)
            plt.scatter(x, y)
            f.write(f"{trueroad},{min_dist_proj}\n")
            print(i)
